#pragma once

#include "CoreMinimal.h"
#include "SpatialCoordinate.h"
#include "LRG_GridCellContainer.generated.h"

class ULRG_Node_GridCell;

USTRUCT(BlueprintType)
struct FLRG_GridCellContainer
{
	GENERATED_BODY()

	static constexpr int32 NumElementsPerChunk = 256 * 256;	//65536=64*1024,一个chunk占512k
	static constexpr int32 NumChunk = 8 * 8;
	static constexpr int32 CoordinateUpper = 8 * 256 / 2; //1024
	//8 * 256 / 2 = 1024
	//格子坐标范围按配置只支持[-1024,-1024],[1024,1024]
	//按一个格子4800,则世界坐标最大扶持4915200
	//在ReplicationGraph中UE_OLD_WORLD_MAX=2097152.0,已经足够使用
public:
	void Initialize(int32 InNumChunk = NumChunk);
	void SetGridCellCreator(const TFunction<ULRG_Node_GridCell*(const FSpatialCoordinate& Coordinate)>& Function);
public:
	ULRG_Node_GridCell* TryCreateGridCell(const FSpatialCoordinate& Coordinate);
	ULRG_Node_GridCell* FindGridCell(const FSpatialCoordinate& Coordinate) const;
	
protected:
	UPROPERTY()
    TSet<ULRG_Node_GridCell*> ReferenceSet;
	
	ULRG_Node_GridCell*** GridCells = nullptr;
	TFunction<ULRG_Node_GridCell*(const FSpatialCoordinate& Coordinate)> GridCellCreator;
};